package com.know.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;

import my.mvc.RequestContext;
import my.util.ResponseUtils;

import com.know.entity.Article;
import com.know.entity.Images;
import com.know.service.ImageService;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;
import com.kzw.json.Jackson;

/**
 * mixed_pic.jsp 所属服务
 * @author Administrator
 *
 */
public class ImageAction {
	private ImageService imageService=new ImageService();
	
	/**
	 * 照片墙页面跳转服务界面
	 */
	public void into(RequestContext rc) throws ServletException, IOException {
		
		
		System.out.println("跳转照片墙页面");
		rc.forward("/mixed_pic.jsp");
	}   
	/**
	 * 流加载照片
	 */
	public void list(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 8);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Images> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = imageService.findPage(page);
		// 输出JSON
		String json = Jackson.me().toJson(record);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	public void down(RequestContext rc) throws ServletException, IOException {
		String filenameid=rc.param("id");
		Images image=imageService.down(filenameid);
		Record record=new Record(false);
		if(image!=null)
		{
			String path=rc.context().getRealPath(image.getImg_path());
			  // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            rc.response().reset();
            // 设置response的Header
            rc.response().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            rc.response().addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream( rc.response().getOutputStream());
            rc.response().setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            record.setSuccess(true);
			
		}
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);

	}

}
